import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //购物车数据
    shoppingDatas: [],
  },
  mutations: {
    /**
     * 添加商品到购物车
     */
    addShoppingData(state, goods) {

      //判断是否有相同商品
      const isExist = state.shoppingDatas.some(item => {
        if (item.id === goods.id) {
          item.number += 1
          return true;
        }
      })
      if (!isExist) {
        Vue.set(goods, 'isCheck', false);
        Vue.set(goods, 'number', 1);
        state.shoppingDatas.push(goods);
      }
    },
    /**
     * 更改变指定的商品数量
     */
    changeShoppingDataNumber(state, data) {
      state.shoppingDatas[data.index].number = data.number;
    }
  },
  actions: {},
  modules: {}
})
